/**
 * Description:
 * Date: 2025-11-08
 * Time: 17:08
 */
//无头单向非循环链表实现
public class SingleLinkedList implements IList {
    //链表节点，理解为一个个的对象组成
    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public int usedSize;
    //头节点
    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
            usedSize++;
            return;
        }
        node.next = head;
        head = node;
        usedSize++;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (cur == null) {
            this.head = node;
            usedSize++;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        usedSize++;
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == usedSize) {
            addLast(data);
            return;
        }
        if (index < 0 || index > usedSize){
            System.out.println("插入位置错误！");
            return;
        }
        ListNode cur = this.head;
        while (index - 1 != 0){
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;
        usedSize++;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                usedSize--;
                return;
            }
            cur = cur.next;
        }

        if (this.head.val == key) {
            this.head = this.head.next;
            usedSize--;
        }

    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null){
            if (cur.val == key) {
                prev.next = cur.next;
                usedSize--;
            }else {
                prev = prev.next;
            }
            cur = cur.next;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            usedSize--;
        }

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        ListNode cur = this.head.next;
        this.head.next = null;
        while (cur != null) {
            cur = cur.next;
            this.head = cur;
            this.head.next = null;
        }
        this.head = null;
        usedSize = 0;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }




}
